$NG.AllReady(function (page, { useValuesChange, useDataIndexChange, useUpdateRows, useUpdateRow }) {
    const mstform = $NG.getCmpApi("p_form_ss_control_m");
    const user = $NG.getUser();
  
    if (mstform) {
      if (page.oType == "add") {
        console.log("page.oType" + page.oType);
        getPtOriInfoByPhid();
      }
    }
  
    useValuesChange(({ args }) => {
      getPtInfoByPhid();
      getPtOriInfoByPhid();
      cntMFilter();
      getCntCount("p_form_ss_control_m", "u_cnt_name", () => {
        getCntMInfoByPhid();
      });
    }, "p_form_ss_control_m.phid_pc");
    useValuesChange(({ args }) => {
      getCntMInfoByPhid();
    }, "p_form_ss_control_m.u_cnt_name");
    if (page.oType == "add") {
      $NG.updateState((updater) => {
        updater.data.p_form_ss_control_m.setProps({
          creator: {
            label: user.userName,
            value: user.userID,
          },
          ng_insert_dt: formatDate(new Date(), "yyyy-mm-dd"),
        });
      });
    }
  
    function getPtOriInfoByPhid(params) {
      const phidPcV = mstform.getItem("phid_pc").getValue();
      if (!phidPcV) return;
      $NG.execServer("getPtInfoByPhid_1", { phid: phidPcV }, function (res) {
        console.log(res);
        if (res.count > 0) {
          const data = JSON.parse(res.data)[0].extendObjects; //单返回值
          console.log(data);
          $NG.updateState((updater) => {
            updater.data.p_form_ss_control_m.setProps({
              u_implementpro_ori: data.user_implementpro,
              u_implementpro: data.user_implementpro,
              u_liquidation_ori: data.user_liquidation,
              u_liquidation: data.user_liquidation,
              //   u_prostop_ori: data.end_date,
              u_fygk_ori: data.user_fygk,
              u_fygk: data.user_fygk,
              u_srbl: data.user_srbl,
            });
          });
        } else {
          console.log("功能扩展暂无数据");
        }
      });
    }
    //获取项目信息
    getPtInfoByPhid();
    function getPtInfoByPhid() {
      if (mstform.getItem("phid_pc").getValue()) {
        $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
          if (res.count == 0) return;
          const datas = JSON.parse(res.data)[0].extendObjects;
          console.log(datas);
          $NG.updateState((updater) => {
            updater.data.p_form_ss_control_m.setProps({
              //项目编码
              u_pc_no: datas.pc_no,
              //计划开工日期
              u_start_dt: datas.start_date,
              //计划完工日期
              u_end_dt: datas.end_date,
              //项目经理
              phid_emp: {
                value: datas.project_manager_phid,
                label: datas.project_manager_name,
              },
              //组织
              phid_org_phid: {
                value: datas.phid_org_phid,
                label: datas.phid_org_name,
              },
              //区域中心
              u_regional_center: {
                value: datas.regional_center_phid,
                label: datas.regional_center_name,
              },
              //产品线
              u_product_line: {
                value: datas.midwife_phid,
                label: datas.midwife_name,
              },
              //客户名称
              u_customer_ent: {
                value: datas.customer_id,
                label: datas.customer_name,
              },
              //项目类型
              u_pc_type: {
                value: datas.type_phid,
                label: datas.type_name,
              },
            });
          });
        });
      }
    }
  
    cntMFilter();
    function cntMFilter() {
      $NG.updateUI((updater, state) => {
        updater.fieldSetForm.p_form_ss_control_m.u_cnt_name.setProps({
          onBeforeOpen: async () => {
            var phid_pc = mstform.getItem("phid_pc").getValue();
            if (!phid_pc) {
              $NG.alert("请先选择项目");
              return false;
            } else {
              return true;
            }
          },
          ...(!!mstform.getItem("phid_pc").getValue() && {
            clientSqlFilter: {
              bill_type: 1, //合同类型1:承包合同|4:分包合同;
              phid_pc: mstform.getItem("phid_pc").getValue(),
            },
          }),
        });
      });
    }
    getCntMInfoByPhid();
    function getCntMInfoByPhid(params) {
      $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_cnt_name").getValue() }, function (res) {
        const datas = JSON.parse(res.data)[0].extendObjects;
        console.log(datas);
        $NG.updateState((updater) => {
          updater.data.p_form_ss_control_m.setProps({
            amt: datas.cnt_sum_vat_fc,
            phid_fcur: {
              value: datas.phid_fcur,
              label: datas.phid_fcur_name,
            },
            exchangerate: datas.exch_rate,
          });
        });
      });
    }
  
    getCntCount("p_form_ss_control_m", "u_cnt_name", () => {
      getCntMInfoByPhid();
    });
    //获取合同数量
    function getCntCount(table, key, fun) {
      if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
      $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
        console.log(res, "count");
        switch (res.count) {
          case 0:
            return;
          case 1:
            const datas = JSON.parse(res.data)[0].extendObjects;
            const temp_map = {};
            temp_map[key] = datas.phid;
            temp_map[`${key}_EXName`] = datas.bill_name;
            $NG.updateState((updater) => {
              updater.data[table].setProps({
                ...temp_map,
              });
              setTimeout(() => {
                if (fun) {
                  fun();
                }
              }, 200);
            });
        }
      });
    }
  
    function formatDate(date, outputFormat) {
      if (isNaN(date)) {
        return "无效的日期字符串";
      }
  
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
  
      let formattedDate;
  
      switch (outputFormat) {
        case "yyyy-mm-dd":
          formattedDate = `${year}-${month}-${day}`;
          break;
        case "yyyy-mm-dd HH:mm":
          formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
          break;
        case "yyyy-mm":
          formattedDate = `${year}-${month}`;
          break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
          formattedDate = "不支持的输出格式";
      }
  
      return formattedDate;
    }
  });
  